<?php

namespace app\sales\controller;

use app\admin\common\Base;
use think\Db;
//销售排行表
class Salerankdetail extends Base
{
   public function index()
   {
      $data = $this->request->param();
      if (isset($data['ajax']) && $data['ajax'] == 100) {
         $map = '';
         $page = isset($data['page']) ? $data['page'] : 1;
         $page = intval($page);
         $limit = isset($data['rows']) ? $data['rows'] : 10;
         $limit = intval($limit);
         $start = $limit * ($page - 1);


         if ($map == '') {
            $list = Db::name('customer')->where('is_customer', 'in', 0, 3)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
            $count = Db::name('customer')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
         } else {
            $list = Db::name('customer')->where('is_customer', 'in', 0, 3)->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
            $count = Db::name('customer')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
         }

         foreach ($list as $key => $value) {
            $list[$key]['xsbs'] = 0;
            $list[$key]['xsbasenum'] = 0;
            $list[$key]['price'] = 0;
            $list[$key]['tax_e'] = 0;
            $list[$key]['total_price'] = 0;
            $list[$key]['dwcb'] = 0;
            $list[$key]['xscb'] = 0;
            $list[$key]['xsml'] = 0;
            $list[$key]['mll'] = 0;
            $list[$key]['category_name'] = Db::name('customer_category')->where('id', $value['category_id'])->value('name');

            $order = Db::name('order')->where('user_id', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            foreach ($order as $k => $v) {
               $list[$key]['xsbs']++;
               $details = Db::name('order_good')->where('order_id', $v['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
               foreach ($details as $k1 => $v1) {
                  $list[$key]['xsbasenum'] += $v1['base_shuliang'];
                  $list[$key]['dwcb'] += Db::name('goods_price_strategy')->where('goods_id', $v1['goods_id'])->value('estimated_purchase_price');
               }
               $list[$key]['price'] = Db::name('order_good')->where('order_id', $v['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->sum('jine') * $list[$key]['xsbasenum'];
               $list[$key]['tax_e'] = Db::name('order_good')->where('order_id', $v['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->sum('tax_e') * $list[$key]['xsbasenum'];
               $list[$key]['total_price'] = Db::name('order_good')->where('order_id', $v['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->sum('total_price') * $list[$key]['xsbasenum'];
            }
            $list[$key]['dwcb'] = $list[$key]['dwcb'] != 0 && $list[$key]['xsbasenum'] != 0 ? round($list[$key]['dwcb'] / $list[$key]['xsbasenum'], 2) : 0;
            $list[$key]['xscb'] = $list[$key]['dwcb'] * $list[$key]['xsbasenum'];
            // 销售毛利
            $list[$key]['xsml'] = $list[$key]['price'] - $list[$key]['xscb'];
            // 毛利率%
            $list[$key]['mll'] = $list[$key]['xsml'] != 0 && $list[$key]['price'] != 0 ? round($list[$key]['xsml'] / $list[$key]['price'] * 100, 2) . '%' : '0.00%';
         }

         $list = $this->all_sort($list, 'xsbs');
         return $this->grid_data($list, $count, $page, $limit);
      }

      return $this->fetch();
   }


   public function add()
   {
      $data = $this->request->param();


      return $this->fetch();
   }
}
